
<?php  include('../../Connections/KRAPPERCON.php')   ?>
<!-- 
<div style="height:10px" id="subtitle" class="subtitle">
    <span class="titactback" id="sp_back" >&#9668; Volver</span> 
    <div class="titact"><?php  echo getParam('accion','');   ?></div>
</div>
<div>
-->
<?php 

$buscar  = false;
$found = false;

$oper = getGet('oper');
$codsrv = '';

if ($oper == 'load'){
	$codsrv = getGet('servicio');
	$buscar = true;
} 
?>

<div>
  <input type="button"  name="btn_nsrv" id="btn_npro" onclick="nuevoServicio()" value="Nuevo" />
  &nbsp;
  <input type="button"  name="btn_esrv" id="btn_epro" onclick="editServicio()" value="Editar" />
  &nbsp;
  <input type="button" onclick="saveServicio()"   name="btn-save" id="btn-save" value="Grabar" />
  &nbsp;
  <input type="button"   name="btn_lsrv" id="btn_lpro" onclick="listServicios()" value="Listado" />&nbsp;&nbsp;<input name="t_bussrv" type='text' id="t_bussrv" onkeypress="introBusSrv(event)" />
&nbsp;</div>

<?php 
    
	$idServ=getParam('idServ','');
	$desServ='';
	$preServ='';
	$fecReg='';
	$userId='';
	$fecReg = '';
	$stsServ = 0;
	$iniServ = '';
	$finServ = '';
	$moneyID = '';
	
    
 ?>


<form name="frmServ" id="frmServ" action="Mantenimientos/Servicios/grabar.php" >
<input type="hidden" name="desServ" id="desServ" value="<?php echo getParam('desServ','');  ?>"  />
<input type="hidden" name="h_pagina" id="h_pagina" value="<?php echo getParam('h_pagina','');  ?>" /> 
<input type="hidden" name="h_totpag" id="h_totpag"  value="<?php echo getParam('h_totpag',''); ?>" />  
<input type="hidden"  name="idServ" id="idServ_h" value="<?php echo $idServ;   ?>"  />
<input type="hidden"  name="opcion" id="opcion" value="<?php echo getParam('opcion','');   ?>"  />
<input type="hidden"  name="title_h" id="title_h" value="<?php echo getParam('title_h',''); ?>"  />
<div id="_st">
 <div class="subTit">
    </div>
</div>
<table border="0" width="100%" id="tbl_dat_srv" cellpadding="0" cellspacing="0" >
  <tr>
    <td class="labelf">Descripción</td>
    <td><input name="desDetSer" id="desDetSer" type="text" class="clsReq" size="40" />  </td>
    <td>Código</td>
    <td>
      <input type="text" name="t_srvid" id="t_srvid" class="disabled" disabled="disabled" />
  </td>
  </tr>
  <tr>
     <td class="labelf">Precio </td>
    <td><input name="impSerMan" id="impSerMan" type="text" class="clsReq qt" onchange="mr(this)" size="15" maxlength="8"></td>
    <td>Moneda</td>
    <td>
      <select name="cb_mon_srv" id="cb_mon_srv">
       <?php include ('../../Query_Min/getMonedas.php'); ?>
      </select>
    </td>
  </tr>
  <tr>
    <td >Validez</td>
    <td colspan="3">
      <input name="t_iniSrv" class="tcal" type="text" id="t_iniSrv" size="15" maxlength="10" />
    &nbsp;&nbsp;a&nbsp;&nbsp;
    <input name="t_finSrv" class="tcal" type="text" id="t_finSrv" size="15" maxlength="10" />
    </td>
    </tr>
  <tr>
    <td class="labelf">&nbsp;</td>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="4">
    
    <table width="100%" border="1" rules="all" id="tbl_cod_srv" class="tableIn" cellspacing="0" cellpadding="0">
    <thead>
      <tr>
        <th scope="col">Código</th>
        <th scope="col">Descripción</th>
        <th scope="col">Cantidad</th>
        <th scope="col">&nbsp;</th>
        </tr>
        </thead>
        <tbody>
        <?php 
		if( $buscar ){
	   	$i = 0;
		$sql="call sp_findServicio('".$codsrv."'); ";
		doEcho($sql);
		$res = $mysqli->exeQuery($sql);
		if($res){	
			while($row = $res->fetch_assoc()){
				if ($i == 0){
				$found = true;
				$desServ=$row['desDetSer'];
				$preServ=$row['impSerMan'];
				$fecReg=$row['fecReg'];
	            $userId=$row['UserID'];
				$moneyID = $row['MoneyID'];
				$iniServ = $row['iniServ'];
				$finServ = $row['finServ'];
				$stsServ = $row['stsServ'];
				$i++;
				}


		?>
    	  <tr class="" onkeypress="introMatSrv(event);">
        <td>
          <input class="a" type="text" name="t_srvcod" title="<?php echo $row['ArticuloID'];?>" value="<?php echo $row['ArticuloID'];?>" id="t_srvcod" /> </td>
        <td class="b"><?php echo $row['ArticuloName'];?></td>
        <td>
          <input class="c qt" onchange="mr(this)" type="text" name="t_srvqty" value="<?php echo $row['Cantidad'];?>" id="t_srvqty" />        </td>
        <td align="center">
          <input type="button" value="X" onclick="delPosMat(this)" />        </td>
        </tr>
        <?php 			
			}
			$res->close();
            $mysqli->next_result();
		}		
	}
	
	?>
     <tr class="trLast noView" onkeypress="introMatSrv(event);">
        <td>
          <input class="a" type="text" name="t_srvcod" value="" id="t_srvcod" /> </td>
        <td class="b"></td>
        <td>
          <input class="c qt" type="text" name="t_srvqty" onchange="mr(this);" value="" id="t_srvqty" />        </td>
        <td align="center">
          <input type="button" value="X" onclick="delPosMat(this)" />        </td>
        </tr>
    </tbody>
    <tfoot>
      <tr>
        <td>
          <input type="button" name="btn_addcm" id="btn_addcm"  onclick="addRowServ()" value="Agregar" />
        </td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      </tfoot>
    </table></td>
    </tr>

</table>


</form>


<script type="application/javascript">
<?php
if ($found){
?>
$('#_st .subTit').html('Servicio <?php echo $codsrv; ?> creado el por <?php echo $userId; ?> el <?php echo $fecReg; ?>');
GEBI('desDetSer').value = '<?php echo $desServ; ?>';
GEBI('impSerMan').value = '<?php echo $preServ;?>';
GEBI('t_iniSrv').value = '<?php echo $iniServ;?>';
GEBI('t_finSrv').value = '<?php echo $finServ;?>';
GEBI('cb_mon_srv').value = '<?php echo $moneyID;?>';
GEBI('t_srvid').value = '<?php echo $codsrv; ?>';
<?php 
}
else {
?>
$('#_st .subTit').remove();
<?php 
}
?>	
	
var _url=$('#frmServ').attr('action');
$('#sp_back').click(function(){
	var _datos=$('#frmServ').serialize();			
	$('#d_main').loadpp('Mantenimientos/Servicios/MantServicios.php',_datos);				
});

function saveServicio(){
	
	if (!$("#frmServ").checkInput()){
	return;
	}
	
	var items = '';
	var ctd_itm = 0;
	var ret = false;
	$('#tbl_cod_srv tbody tr').not(".trLast").each(function(){
		if(ret==true){
			return;
		}
		var fila = $(this);
		var code = fila.find('.a');
		if ($.trim(code.val())==code.attr('title')){
			var id = fila.find('.a').valx();
			var ctd = pInt(fila.find('.c').valx());
			if (ctd<=0){
			//fila.find('.c').val('').focusError();
			alert('Debe indicar la cantidad para el material '+id);
			ret = true;
			return;
			}
			items = items + id + '|' + ctd +'#';
			ctd_itm++;
		}
		else {
		fila.remove();
		}
	});
	
	if (ret){
	return;
	}
	
	if (ctd_itm == 0){
		showOk("No hay componentes registrados para el Servicio");
		addRowServ();
		return;
	}
	
	sendData(_url,
	{
		oper : 'save',
		idServ : GEBI('t_srvid').value,
		txServ : GEBI('desDetSer').value,
		prServ : GEBI('impSerMan').value,
		moneda : GEBI('cb_mon_srv').value,
		iniSrv : GEBI('t_iniSrv').value,
		finSrv : GEBI('t_finSrv').value,
		status : 1,
		items : items
	});
	
	
}


function addRowServ(){
		var nueva_fila = $('#tbl_cod_srv tbody .trLast').clone();
		nueva_fila.removeClass('noView');
		nueva_fila.removeClass('trLast');		
        $("#tbl_cod_srv tbody").append(nueva_fila);	
}

function nuevoServicio(){
   loadPage('Mantenimientos/Servicios/adicionar.php');
}

function editServicio(){
	$("#tbl_dat_srv").disableAll();
}

function noEditServicio(){
	$("#tbl_dat_srv").enableAll();
}

function delPosMat(obj){
	var fila=$(obj).parent().parent();
	fila.remove();
}

function introMatSrv(e){
	if (e.keyCode!=13){
	return;
	}
	var items = '';
	$('#tbl_cod_srv tbody tr').not(".trLast").each(function(){
		var fila = $(this);
		var code = fila.find('.a');
		if ($.trim(code.val())==''){
			fila.find('.a').html('');
			fila.find('.b').html('');
			fila.find('.c').val('');
			return;
		}
		
		if (code.attr('title')!=code.val()){
		// Buscar el Codigo
			code.txtLoad();
			sendData('Query_Min/getDatArticulo.php',{t_codart : code.val() , field : 'venta'},function(){
			var mobj  = eval('(' + $('#d_result').html() + ')');
			fila.find('.a').val(mobj.code);
			fila.find('.b').html(mobj.desc);
			fila.find('.c').val('');
			code.attr('title',mobj.code);
			code.etxtLoad();
			//setTimeout(fn,0);
			});
		}
	});
}

function listServicios(){
	loadPage('Mantenimientos/Servicios/MantServicios.php');
}

function introBusSrv(e){
	if (e.keyCode!=13){
	return;
	}
	
	loadPage('Mantenimientos/Servicios/adicionar.php',{ oper : 'load', servicio : GEBI('t_bussrv').value});
}

<?php 
	if (!$buscar || !$found){
	?>
	addRowServ();
	<?php
	}
?>

</script>

